package ca.badenhorst.ourtrition;

import android.app.Application;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import ca.badenhorst.ourtrition.model.DVValue;
import ca.badenhorst.ourtrition.model.DVValues;


/**
 * Created by wade on 19/03/14.
 */
public class NutriApplication extends Application {
    public static final String VIT_MIN_DV_CSV = "vit_min_dv.csv";
    DVValues dvValues = new DVValues();

    @Override
    public void onCreate(){
        try {
            InputStreamReader is = new InputStreamReader(getApplicationContext().getAssets().open(VIT_MIN_DV_CSV));
            BufferedReader reader = new BufferedReader(is);
            String line = null;
            while ((line=reader.readLine())!=null){
                parseLine(dvValues, line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private void parseLine(DVValues dvValues, String line) {
        if (line == null || line.startsWith("--")){
            System.out.println("not processing line: "+line);
            return;
        }
        String[] items = line.split(",");
        if (items.length!=3)
        {
            System.out.println("did not find 3 items in line: "+line);
            return;
        }
        DVValue value = new DVValue();
        value.setDesc(items[0]);
        value.setTag(items[1]);
        value.setValue(Double.parseDouble(items[2].substring(0,items[2].lastIndexOf(" ")).trim()));
        value.setUnits(items[2].substring(items[2].lastIndexOf(" ")).trim());
        dvValues.addDVValue(value);
    }

    public DVValues getValues(){
        return dvValues;
    }
}
